package com.duowei.dw_pos.dialog;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatDialogFragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.duowei.dw_pos.R;
import com.duowei.dw_pos.event.CartRemoteUpdateEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

/**
 * A simple {@link Fragment} subclass.
 */
public class NumInputDialogFragment extends AppCompatDialogFragment
        implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {

    public EditText mEtInput;
    public Button mConfirm;
    private Button mCancel;
    public String orderStytle="";

    private RadioButton mRb1;
    private RadioButton mRb2;
    private RadioButton mRb3;
    private ProgressBar mPb;
    public NumInputDialogFragment() {
        // Required empty public constructor
    }

    public interface ConfirmListener{
        void OnConfirmListern(String tableNum,String orderStytle);
    }
    private ConfirmListener mConfirmListener;
    public void setOnConfirmListener(ConfirmListener onConfirmListener){
        mConfirmListener=onConfirmListener;
    }

    public static NumInputDialogFragment newInstance() {
        Bundle args = new Bundle();
        NumInputDialogFragment fragment = new NumInputDialogFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_num_input_dialog, container, false);
        mPb = (ProgressBar) inflate.findViewById(R.id.progressBar);
        mEtInput=(EditText)inflate.findViewById(R.id.et_input);
        mConfirm=(Button)inflate.findViewById(R.id.btn_confirm);
        mCancel=(Button)inflate.findViewById(R.id.btn_cancel);
        mConfirm.setOnClickListener(this);
        mCancel.setOnClickListener(this);
        RadioGroup rg = (RadioGroup) inflate.findViewById(R.id.rg);
        rg.setOnCheckedChangeListener(this);
        mRb1 = (RadioButton) inflate.findViewById(R.id.rb1);
        mRb2 = (RadioButton) inflate.findViewById(R.id.rb2);
        mRb3 = (RadioButton) inflate.findViewById(R.id.rb3);
        mRb1.setChecked(true);
        return inflate;
    }

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe
    public void commitResult(CartRemoteUpdateEvent event){
        getActivity().runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mPb.setVisibility(View.GONE);
            }
        });
        if(event.equals("success")){
            dismiss();
        }else{
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mConfirm.setEnabled(true);
                }
            });
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_cancel:
                dismiss();
                break;
            case R.id.btn_confirm:
                if(TextUtils.isEmpty(mEtInput.getText().toString().trim())){
                    Toast.makeText(getActivity(),"请输入餐牌号",Toast.LENGTH_SHORT).show();
                    return;
                }
                mConfirmListener.OnConfirmListern(mEtInput.getText().toString().trim(),orderStytle);
                mPb.setVisibility(View.VISIBLE);
                mConfirm.setEnabled(false);
                dismiss();
                break;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
        if(checkedId==mRb1.getId()){
            orderStytle="堂食";
        }else if(checkedId==mRb2.getId()){
            orderStytle="外带";
        }else if(checkedId==mRb3.getId()){
            orderStytle="外送";
        }
    }
}
